package com.group3.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group3.dao.AttendanceResultMapper;
import com.group3.domain.*;
import com.group3.pojo.receive.AttendanceReceive;
import com.group3.pojo.result.AttendanceInfoResult;
import com.group3.service.StudentAttendanceService;
import com.group3.utils.NotNull;
import com.group3.utils.Result;
import com.group3.utils.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * StudentAttendanceServiceImpl
 *
 * @author HY
 * 2021/06/10 16:57
 **/


@Service
public class StudentAttendanceServiceImpl implements StudentAttendanceService {
    @Resource
    private AttendanceResultMapper resultMapper;

    @Override
    public Result<List<AttendanceResult>> getAttendance(
            AttendanceReceive attendanceReceive) {
        AttendanceResultExample resultExample = new AttendanceResultExample();
        AttendanceResultExample.Criteria criteria = resultExample.createCriteria();
        criteria.andStudentIdEqualTo(attendanceReceive.getStudentId());

        if (NotNull.judge(attendanceReceive.getLessonName())) {//传入了课程名
            criteria.andLessonNameLike(attendanceReceive.getLessonName());
        }
        if (NotNull.judge(attendanceReceive.getStartTime())) {//传入了日期
            criteria.andDateGreaterThanOrEqualTo(attendanceReceive.getStartTime());
        }
        if (NotNull.judge(attendanceReceive.getEndTime())) {//传入了日期
            criteria.andDateLessThanOrEqualTo(attendanceReceive.getEndTime());
        }
        if (NotNull.judge(attendanceReceive.getType())) {
            criteria.andAttendanceTypeEqualTo(attendanceReceive.getType());
        }
        PageHelper.startPage(attendanceReceive.getPageNumber()
                , attendanceReceive.getPageSize());//启动分页
        List<AttendanceInfoResult> attendanceResults
                = resultMapper.selectAttendanceInfo(resultExample);
        PageInfo<AttendanceInfoResult> resultPageInfo
                = new PageInfo<>(attendanceResults);//分页信息
        return ResultUtil.success(attendanceResults, resultPageInfo.getTotal());
    }
}
